#include "fmc.h"

uint8_t fmc_write(uint32_t address, uint8_t *data, uint32_t num)
{
	 uint32_t i = 0;
	 /* unlock the option byte operation */
   fmc_unlock();
	 /* clear pending flags */
	 fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_OPERR | FMC_FLAG_WPERR | FMC_FLAG_PGMERR | FMC_FLAG_PGSERR);
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_4))
		 return 1;
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_5))
		 return 1;
	 
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_6))
		 return 1;
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_7))
		 return 1;
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_8))
		 return 1;
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_9))
		 return 1;
	 if(0 != fmc_sector_erase(CTL_SECTOR_NUMBER_10))
		 return 1;
	 for(i = 0; i < num; i++)
	 {
		 if(0 != fmc_byte_program(address++, data[i]))
			 return 1;
	 }
	 /* lock the option byte operation */
	 fmc_lock();
	 return 0;
}
